今天我會利用之前所學去實際演練一遍
下列為操作的步驟 :
程式碼 :
import json
import os
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException, TimeoutException
def login_process(driver):
"""處理登入流程"""
try:
# 點擊登入按鈕
login_button = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, '.member-login-button__LoginButton-sc-6620974f-1.htFhOU'))
)
login_button.click()
# 輸入帳號
account = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, '.generic-text-input__Input-sc-435be10e-1.ixoMWw'))
)
account.send_keys('輸入你的帳號')
# 點擊下一步
next_button = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, '.primary-button__Button-sc-cb5d7fe-0.eqQzvt'))
)
next_button.click()
# 輸入密碼
password = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, '.generic-password-input__Input-sc-89985254-2.fGyVfa'))
)
password.send_keys('輸入你的密碼')
# 點擊登入
login_button = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, '.primary-button__Button-sc-cb5d7fe-0.eqQzvt'))
)
login_button.click()
# 確認登入成功
WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CSS_SELECTOR, '.slug-__SectionContainer-sc-417d02b-0.gLXTDh'))
)
time.sleep(5)
print('登入成功')
except (NoSuchElementException, TimeoutException) as e:
print(f"登入過程中發生錯誤: {e}")
def navigate_to_subscription_record(driver):
"""導航到訂閱紀錄頁面"""
try:
# 等待會員圖標可見並點擊
member_icon = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, '.premium-member-login-button__LoggedInWrapper-sc-2ae1715e-2.gbFusZ img[alt="member-icon-logged-in"]'))
)
member_icon.click()
# 確保下拉菜單顯示
dropdown_menu = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.CSS_SELECTOR, '.premium-member-login-button__DropdownMenu-sc-2ae1715e-3.dGtaFu'))
)
# 點擊「訂閱紀錄」鏈接
subscription_record = dropdown_menu.find_element(By.LINK_TEXT, '訂閱紀錄')
subscription_record.click()
# 等待頁面加載完成
WebDriverWait(driver, 10).until(EC.url_contains('/profile/purchase'))
print("已導航到訂閱紀錄頁面。")
time.sleep(3)
except (NoSuchElementException, TimeoutException) as e:
print(f"導航過程中發生錯誤: {e}")
def navigate_to_profile(driver):
"""導航到個人資料頁面"""
try:
# 再次等'訂閱紀錄頁面'的會員圖標可見並點擊
member_icon = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, '.logged-in-icon'))
)
member_icon.click()
# 確保下拉菜單顯示
dropdown_menu = WebDriverWait(driver, 10).until(
EC.visibility_of_element_located((By.CSS_SELECTOR, '.logged-in-wrapper__dropdown-menu.dropdown-menu'))
)
# 點擊「個人資料」鏈接
profile_link = dropdown_menu.find_element(By.LINK_TEXT, '個人資料')
profile_link.click()
WebDriverWait(driver, 10).until(EC.url_contains('/profile'))
print("已導航到個人資料頁面。")
time.sleep(3)
except (NoSuchElementException, TimeoutException) as e:
print(f"導航過程中發生錯誤: {e}")
def navigate_to_logout(driver):
"""從個人資料頁面登出"""
# 再次等'個人資料頁面'的會員圖標可見並點擊
m_icon = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, '.member-login-button__LoggedInWrapper-sc-6620974f-2.eFqSgk'))
)
m_icon.click()
# 點擊「登出」鏈接
log_out = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable((By.LINK_TEXT, '登出'))
)
log_out.click()
print("已登出")
# 滾動到頁面底部
driver.find_element(By.TAG_NAME, 'body').send_keys(Keys.END)
time.sleep(3)
def main():
# 初始化 WebDriver
driver = webdriver.Chrome()
driver.get('https://www.mirrormedia.mg/')
# 登入流程
login_process(driver)
# 確保頁面完全加載
time.sleep(5)
# 確認登入成功並進入訂閱紀錄頁面 , 在進入個人資料頁面
navigate_to_subscription_record(driver)
navigate_to_profile(driver)
navigate_to_logout(driver)
# 最終關閉 WebDriver
driver.quit()
if __name__ == "__main__":
main()
參考資料 :
https://jenifers001d.github.io/2019/12/23/Python/learning-Python-day15/